//
//  RHFirmwareUpgradeCell.m
//  RuiHeAppApp
//
//  Created by Jun on 2023/8/4.
//

#import "RHFirmwareUpgradeCell.h"

@interface RHFirmwareUpgradeCell()

//固件标题
@property (nonatomic ,strong) QMUILabel *titleLab;
//固件版本
@property (nonatomic ,strong) QMUILabel *versionLab;
//文件大小
@property (nonatomic ,strong) QMUILabel *sizeLab;
//固件描述
@property (nonatomic ,strong) QMUILabel *describeLab;

@property (nonatomic ,strong)UIImageView *markImg;

@property (nonatomic ,strong)UIImageView *firmwareIcon;

@property (nonatomic ,strong) MASConstraint *left_constraint;
@end


@implementation RHFirmwareUpgradeCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        [self setupUI];
    }
    return self;
}
-(void)setFirmwareModel:(RHFirmwareModel *)firmwareModel
{
    _firmwareModel = firmwareModel;
    self.describeLab.text = firmwareModel.describe_lan;
    self.versionLab.text = [NSString stringWithFormat:@"%@:%@",@"版本".rh_localized,ValidNullDataToString(firmwareModel.firmwareNumber, @"--")];
    
    NSString *sizeStr = [NSString stringWithFormat:@"%@:--",@"固件大小".rh_localized];
    if([firmwareModel.size integerValue]>1000){
        CGFloat size = [firmwareModel.size integerValue]/1024.0;
        sizeStr = [NSString stringWithFormat:@"%@:%@MB",@"固件大小".rh_localized,[@(size) twoDecimalPlacesWithoutRounding]];
    }else if ([firmwareModel.size integerValue]>0){
        
        sizeStr = [NSString stringWithFormat:@"%@:%ldKB",@"固件大小".rh_localized,[firmwareModel.size integerValue]];
    }
    
    self.sizeLab.text = sizeStr;
    
    self.markImg.image = [UIImage imageNamed:firmwareModel.isSelected?@"ugrade_select":@"ugrade_cancel"];
    self.markImg.hidden = (!firmwareModel.isClicked) || (firmwareModel.normal == 2) || (firmwareModel.normal == 0);
    if(firmwareModel.normal == 2){//固件异常
        self.titleLab.textColor = kCOLOR_HEX(@"FA3040");
        self.titleLab.text = [NSString stringWithFormat:@"%@(%@)",firmwareModel.endpoint,@"固件异常?".rh_localized];
    }else{
        self.titleLab.textColor = kCOLOR_HEX(@"323233");
        self.titleLab.text = ValidNullDataToString(firmwareModel.endpoint, @"--");
    }
    kWeakSelf;
    
    if(firmwareModel.isClicked){
       
        if(self.left_constraint){
            [self.left_constraint uninstall];
        }
        
        [self.firmwareIcon mas_updateConstraints:^(MASConstraintMaker *make) {
            self.left_constraint = make.left.equalTo(weakSelf.markImg.mas_right).offset(10);
        }];
        
    }else{
        if(self.left_constraint){
            [self.left_constraint uninstall];
        }
        [self.firmwareIcon mas_makeConstraints:^(MASConstraintMaker *make) {
           self.left_constraint = make.left.mas_offset(15);
        }];
    }
        
    
    if(firmwareModel.normal == 2){
        if(self.left_constraint){
            [self.left_constraint uninstall];
        }
        [self.firmwareIcon mas_makeConstraints:^(MASConstraintMaker *make) {
           self.left_constraint = make.left.mas_offset(15);
        }];
    }
}

- (void)setupUI{
    kWeakSelf;
    [self.contentView setBackgroundColor:CFTabViewBgColor];
    [self setBackgroundColor:CFTabViewBgColor];
    
    UIView *bgView = [[UIView alloc] init];
    [bgView.layer setCornerRadius:8.0];
    [bgView setBackgroundColor:UIColor.whiteColor];
    [self.contentView addSubview:bgView];
    
    UIImageView *firmwareIcon = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"kwt_firmware_icon"]];
    [bgView addSubview:firmwareIcon];
    self.firmwareIcon = firmwareIcon;
    
    _titleLab = [[QMUILabel alloc] init];
    [_titleLab setTextAlignment:NSTextAlignmentLeft];
    [_titleLab setTextColor:kCOLOR_HEX(@"323233")];
    [_titleLab setFont:[UIFont boldSystemFontOfSize:15]];
    [bgView addSubview:_titleLab];
    
    UIView *versionSizeView = kView;
    [bgView addSubview:versionSizeView];
    [versionSizeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.titleLab);
        make.right.mas_offset(-3);
        make.top.equalTo(weakSelf.titleLab.mas_bottom).offset(10);
    }];
    
    _versionLab = [[QMUILabel alloc] init];
    [_versionLab setTextAlignment:NSTextAlignmentLeft];
    [_versionLab setTextColor:kCOLOR_HEX(@"323233")];
    [_versionLab setFont:[UIFont systemFontOfSize:12]];
    _versionLab.numberOfLines = 0;
    [versionSizeView addSubview:_versionLab];
    
    _sizeLab = [[QMUILabel alloc] init];
    [_sizeLab setTextAlignment:NSTextAlignmentLeft];
    [_sizeLab setTextColor:kCOLOR_HEX(@"323233")];
    [_sizeLab setFont:[UIFont systemFontOfSize:12]];
    _sizeLab.numberOfLines = 0;
    [versionSizeView addSubview:_sizeLab];
    
    UIImageView *markV = [[UIImageView alloc] init];
    [markV setImage:[UIImage imageNamed:@"ugrade_select"]];
    [bgView addSubview:markV];
    markV.hidden = YES;
    self.markImg = markV;
    
    UIView *desBgView = kView;
    desBgView.backgroundColor = CFTabViewBgColor;
    desBgView.layer.cornerRadius = 8;
    desBgView.layer.masksToBounds = YES;
    [bgView addSubview:desBgView];
    [desBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(firmwareIcon);
        make.right.equalTo(weakSelf.titleLab);
        make.top.equalTo(versionSizeView.mas_bottom).offset(10);
        make.bottom.mas_offset(-15);
    }];
    
    _describeLab = [[QMUILabel alloc] init];
    [_describeLab setTextAlignment:NSTextAlignmentLeft];
    [_describeLab setTextColor:[kCOLOR_HEX(@"323233") colorWithAlphaComponent:0.6]];
    [_describeLab setFont:[UIFont systemFontOfSize:12]];
    [_describeLab setNumberOfLines:1];
    [desBgView addSubview:_describeLab];
    
    
    QMUIButton *detailBtn = [QMUIButton buttonWithType:UIButtonTypeCustom];
    [detailBtn setImage:[UIImage imageNamed:@"kwt_frimware_detail"] forState:UIControlStateNormal];
    [bgView addSubview:detailBtn];
    
    
    
    
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(10, 15, 5, 15));
    }];
    
    [firmwareIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(25, 25));
        make.top.mas_offset(15);
        weakSelf.left_constraint = make.left.mas_offset(15);
        
    }];
    
    [_titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(firmwareIcon.mas_right).offset(10);
        make.top.equalTo(firmwareIcon).offset(0);
        make.height.mas_equalTo(15);
        make.right.mas_offset(-15);
    }];
    
    [_versionLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.mas_offset(0);
        make.width.lessThanOrEqualTo(@(120));
    }];
    
    [_sizeLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.versionLab.mas_right).offset(10);
        make.top.right.bottom.mas_offset(0);
    }];
    
    [markV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(20, 20));
        make.left.mas_offset(15);
        make.centerY.mas_offset(0);
    }];
    
    [_describeLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.top.mas_offset(15);
        make.right.mas_offset(-15);
        make.bottom.mas_offset(-15);
    }];
    
   
    
    [detailBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(5);
        make.right.mas_offset(-5);
        make.width.height.offset(35);
    }];
    
   
    
    
    
    [[detailBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        if(weakSelf.learnMoreBlock){
            weakSelf.learnMoreBlock();
        }
    }];
    
    [_titleLab setText:@"EMS 1.9.2"];
    [_versionLab setText:@"Smart Energy"];
    [_sizeLab setText:@"20.5M"];
    [_describeLab setText:@"Firmware Upgrade Firmware Upgrade Firmware Upgrade Firmware Upgrade"];
}




- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
